using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapInfo.MiPro.Interop;
using System.Windows.Forms;
using XtremeDockingPane;
using AxXtremeDockingPane;
using System.Drawing;
using MapWindowDocking.Mapinfo;
using MapWindowDocking.Mapinfo.Enums;

namespace MapWindowDocking
{
	/// <summary>
	/// A wrapper around the COM MapinfoApplication class.
	/// </summary>
	public class MapinfoWrapper
	{
		public MapInfoApplication innermapinfo;
		
		private MapinfoWrapper(MapInfoApplication mapinfo)
		{
			this.innermapinfo = mapinfo;
		}

        public static MapinfoWrapper GetMapinfoServiceFromInterop()
		{
            return new MapinfoWrapper(InteropServices.MapInfoApplication);
		}

		/// <summary>
		/// Returns true if the window with the supplied ID is a map window.
		/// </summary>
		/// <param name="WindowId">The ID of the window to check.</param>
		/// <returns>True if window if map window.</returns>
		public bool WindowIsMapWindow(int WindowId)
		{
            return new Window(WindowId, this).Type == WindowTypes.mapper;
		}
		
		/// <summary>
		/// Returns a new <see cref="Window"/> of the current front window.
		/// </summary>
		/// <returns></returns>
		public Window GetFrontWindow()
		{
			string strwindowid = this.innermapinfo.Eval("FrontWindow()");
            int id = Convert.ToInt32(strwindowid);
			return new Window(id,this);
		}

		
		/// <summary>
		/// Runs the supplied command in Mapinfo.
		/// </summary>
		/// <param name="cmd">The mapbasic string expression to run in Mapinfo.</param>
		public void Do(string cmd)
		{
			this.innermapinfo.Do(cmd);
		}
		
		/// <summary>
		/// Evaluates the supplied Mapbasic string in Mapinfo and returns the result as a string.
		/// </summary>
		/// <param name="cmd">The mapbasic string expression to run in Mapinfo.</param>
		/// <returns>A string contain the result from the expression.</returns>
		public string Eval(string cmd)
		{
			return this.innermapinfo.Eval(cmd);
		}

        public string SessionCoordSys
        {
            get
            {
                return this.Eval(String.Format("SessionInfo({0})", (int)SessionInfoTypes.COORDSYS_CLAUSE));
            }
            set
            {
                string newstring = value;
                if (value.Contains("CoordSys"))
                {
                    newstring = value.Replace("CoordSys", "");
                }
                this.Do("Set CoordSys " + newstring);
            }
        }

        public string SessiongDistanceUnits
        {
            get 
            {
                return this.Eval(String.Format("SessionInfo({0})", (int)SessionInfoTypes.DISTANCE_UNITS));
            }
            set 
            {
                this.Do("Set Distance Units " + value.InQuotes());
            }
        }
	}
}
